<?php

    require_once("SmartyReady.php");
    require_once("usuario.php");
    require_once("mp3.php");
    // preparem l'smarty
    $sm = new SmartyReady();
    $sm->muntarTemplates();
    // preparem la sessio
    session_start();

    if (!empty($_SESSION['username'])) {
        $sm->assign('banner','bannerUser.tpl');
        $sm->assign('usuari',$_SESSION['username']);
        $sm->assign('pathAvatar',$_SESSION['avatar']);
        $sm->assign('cuerpo','editarmp3.tpl');
        $sm->assign('playlist',false);
        // Aconseguim les cançons de l'usuari, les parsejem i muntem un array 
        $user = Usuario::findByUsername($_SESSION['username']);
        $playlist = $user->songs();
        $playlist_raw = Array();
        $playlist_tpl = Array();
        $index = 0;
        if (count($playlist) > 0) {
            foreach($playlist as $song) {
                $playlist_raw[]['title'] = $song->get_titol();
                $playlist_raw[$index]['author'] = $song->get_artista();
                $playlist_raw[$index]['id'] = $song->get_id();
                $playlist_tpl[] = $playlist_raw[$index]['title'] . ' - ' . $playlist_raw[$index]['author'];

                $index++;
            }
        }
        $sm->assign('playlist',$playlist_tpl);
    }
    else {
        // T'has colat a la pagina XXX sense estar logejat
        $sm->assign("banner","login.tpl");
        $sm->assign('resultatLogin','');
        $sm->assign('cuerpo','buit.tpl');
        ?><meta http-equiv="Refresh" content="0;url=./index.php"><?php
    }
    $sm->display("E:/wamp/www/JediMusic/Smarty/templates/base.tpl");
?>